<template>
  <div v-loading="loading" element-loading-text="模型生成中...">
    <div style="width: 280px; border: 1px solid white;">
      <el-form  ref="formRef" label-width="90px">
        <el-form-item label="花键类型">
          <el-select size="small" v-model="type" placeholder="请选择活动区域">
            <el-option label="内花键" value="in"></el-option>
            <el-option label="外花键" value="out"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模数">
          <el-input size="small" v-model.number="m" type="number" onkeypress="return (event.charCode >= 48 && event.charCode <= 57) || event.charCode== 46" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="齿数" >
          <el-input size="small" v-model.number="z" type="number"  onkeypress="return event.charCode >= 48 && event.charCode <= 57" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="压力角">
          <el-select size="small" v-model="alpha">
            <el-option v-for="(item,index) in pressureAngleOption" :key="item" :label="item + '°'"
                       :value="item">

            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="长度">
          <el-input size="small" v-model.number="alength" onkeypress="return event.charCode >= 48 && event.charCode <= 57" type="number" placeholder="请输入内容">
            <i slot="suffix" style="font-style:normal; color: #606266;">mm</i>
          </el-input>
        </el-form-item>
        <el-form-item label="齿根">
          <el-radio-group size="small" v-model="rootType">
            <el-radio size="small" label="圆齿根">圆齿根</el-radio>
            <el-radio size="small" label="平齿根">平齿根</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div>
        <img src="/image/spline.png" height="135" width="280"/>
        <div style="margin-top: 10px;text-align: center">
          <el-button type="primary" size="small" @click="creatSpline">确定</el-button>
          <el-button size="small" @click="closeSplineDialog">取消</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {
    Axis,
    CrownCADPlugin,
    Direction, PluginCADToolFunctionType,
    PluginFunctionType,
    Point
  } from "../../../../nodeApp/static/lib/crowncad-plugin-sdk-0.0.1";
  export default {
    name: 'Spline',
    data() {
      return {
        loading:false,
        type: 'out',   // 花键类型
        m: 0.75,   // 模数
        z: 20,  // 牙数
        alpha: 30,   // 压力角
        rootType: '平齿根',  // 齿根
        alength:10,  // 长度
        crownCADPlugin: null,
        currentPickElement:null,
        d: 0, //分度圆直径D = m*z
        db: 0, //基圆Db = m*z*Cos(α)
        dee: 0, //平齿根大径Dee = m*(z+1)
        die: 0,  //平齿根小径Die = m*(z-1.5)
        dei: 0, //平齿根内花键大径Dei = m*(z+1.5)
        cf: 0, //平齿根齿形裕度CF = 0.1*m
        dii: 0, //平齿根内花键小径Dii=DFemax+2*CF
        diee: 0, //圆齿根小径Diee = m*(z-1.8)
        deii: 0, //圆齿根内花键大径Deii = m*(z+1.8)
        pressureAngle: 0, //压力角换算弧度
        DFemax: 0,  //外花键渐开线起始圆直径最大值
        hs:0, //DFemax计算中的参数
        q1: {}, //渐开线与分度圆交点
        q2: {}, //内花键两天渐开线交点
        //压力角
        pressureAngleOption: [20,25,30,37.5,45], //压力角字典
        mP:0, // 以渐开线与分度圆交点构建内嵌多边形，多边形一边的中点，该中点构建镜像线
        EquationId:0, // 第一条渐开线id
        EquationP: {}, // 第一条渐开线终点
        EquationS:{}, // 第一条渐开线起点
        EquationId1:0, // 第二条渐开线id
        EquationP1: {}, // 第二条渐开线终点
        EquationS1:{}, // 第二条渐开线起点
        axisId:0, // 镜像轴id
        axisLen:0, // 镜像轴长度
        id1: [], // 分度圆id
        id2: [], // 内嵌多边形除了与镜像轴交点的所有边的id
        deeid: [], // 平齿根大径Dee的id
        dieid:[], // 平齿根小径Die的id
        deiid:[], // 平齿根内花键大径Dei的id
        deiiid:[], // 圆齿根内花键大径Dei的id
        diiid:[], //平齿根内花键小径Dii的id
        dieeid:[], //圆齿根小径Diee的id
        poel:[], //构建圆齿根时，存储小径或者大径（平齿根和圆齿根）与两条渐开线交点的那两段曲线
      }
    },
    mounted() {
      //创建插件对象
      this.crownCADPlugin = new CrownCADPlugin();
      //设置CrownCAD平台地址
      this.crownCADPlugin.setParentOrigin(window.url);
      //.设置与CrownCAD平台尝试建立连接的次数
      this.crownCADPlugin.setMaxAttempt(5);
      //设置插件在CrownCAD平台显示的尺寸
      this.crownCADPlugin.setLayout(300, 300, 530);
      //.发送与CrownCAD平台建立连接的请求

      this.crownCADPlugin.connect().then(() => {
        this.crownCADPlugin.request.execFunction(PluginFunctionType.LAYOUT, {minWidth: 300, width: 300, maxHeight: 480})
      });
    },
    methods: {
      closeSplineDialog() {
        this.crownCADPlugin.request.execFunction(PluginCADToolFunctionType.CLOSEACTIVECMD, null);
      },
      // 准备大径小径等画草图数据
      updateData() {
        this.pressureAngle = (this.alpha / 180) * Math.PI;
        this.d = this.m * this.z;
        this.db = this.m * this.z * Math.cos(this.pressureAngle);
        this.hs = 0.6* this.m;
        this.DFemax=2* Math.sqrt(Math.pow(0.5*this.db,2)+ Math.pow(0.5* this.d* Math.sin(this.pressureAngle)- this.hs/Math.sin(this.pressureAngle), 2));


        this.dii= this.DFemax+ 2* this.cf;
        this.dee= this.m * (this.z + 1);
        this.die= this.m * (this.z - 1.5);
        this.dei= this.m * (this.z + 1.5);
        this.cf = 0.1 * this.m;
        this.diee= this.m* (this.z- 1.8);
        this.deii= this.m* (this.z+ 1.8)

        this.id1= []
        this.id2= []
        this.deeid= []
        this.dieid=[]
        this.deiid=[]
        this.deiiid=[]
        this.diiid=[]
        this.dieeid=[]
        this.poel=[]
      },

      //创建花键函数
      creatSpline() {
        this.loading = true;

        //获取新配置，重新计算准备各个参数
        this.updateData()

        //清空command
        this.crownCADPlugin.command.clearCommand();
        //创建草图，命名为sketch1
        this.crownCADPlugin.command.sketch.createSketch({sketchName: 'sketch1', datumId: 8});
        //创建圆函数，草图中画几个基本圆
        this.createCircle()
        //画渐开线
        this.crownCADPlugin.command.sketch.createEquationCurve({
          equationType: 2,
          coordinateType: 1,
          equation1: 0.5* this.db + "*(sin(t) - t*cos(t))",
          equation2: 0.5* this.db + "*(cos(t) + t * sin(t))",
          startValue: 0,
          endValue: 1
        });
        //退出草图
        this.crownCADPlugin.command.sketch.exitSketch();
        //实现草图操作，参数true会覆盖之前的草图，不传则不会
        this.crownCADPlugin.command.execute(true).then(() => {
          this.crownCADPlugin.command.clearCommand();
          //获取上面画的圆和渐开线的信息
          this.getCircleAndEquationCurveInfo().then(()=>{
            //剪切分度圆，因为要取分度圆和渐开线的交点，分度圆裁剪后会剩两者的交点
            this.cutPitchCircle();
          });

        })
      },

      //创建圆函数，草图中画几个基本圆
      createCircle() {
        // 分度圆
        this.crownCADPlugin.command.sketch.createCircle({
          centerPoint: new Point(0,0,0),
          circlePoint: new Point(0,0.5* this.d,0),
        })
        //齿顶圆
        if(this.type=== 'out') {

          this.crownCADPlugin.command.sketch.createCircle({
            centerPoint: new Point(0,0,0),
            circlePoint: new Point(0,0.5* this.dee,0),
          })
        } else {
          this.crownCADPlugin.command.sketch.createCircle({
            centerPoint: new Point(0,0,0),
            circlePoint: new Point(0,0.5* this.dii,0),
          })
        }
        // 齿根圆
        if(this.type=== 'out') {
          if(this.rootType=== '平齿根') {
            this.crownCADPlugin.command.sketch.createCircle({
              centerPoint: new Point(0,0,0),
              circlePoint: new Point(0,0.5* this.die,0),
            })
          } else {
            this.crownCADPlugin.command.sketch.createCircle({
              centerPoint: new Point(0,0,0),
              circlePoint: new Point(0,0.5* this.diee,0),
            })
            this.crownCADPlugin.command.sketch.createCircle({
              centerPoint: new Point(0,0,0),
              circlePoint: new Point(0,0.5* this.die,0),
            })
          }

        } else {
          if(this.rootType=== '平齿根') {
            this.crownCADPlugin.command.sketch.createCircle({
              centerPoint: new Point(0,0,0),
              circlePoint: new Point(0,0.5* this.dei,0),
            })
          } else {
            this.crownCADPlugin.command.sketch.createCircle({
              centerPoint: new Point(0,0,0),
              circlePoint: new Point(0,0.5* this.dei,0),
            })
            this.crownCADPlugin.command.sketch.createCircle({
              centerPoint: new Point(0,0,0),
              circlePoint: new Point(0,0.5* this.deii,0),
            })
          }
        }
      },

      //获取上面画的圆和渐开线的信息
      getCircleAndEquationCurveInfo() {
        return new Promise((resolve, reject) => {
          //根据草图名称sketch1，获取草图信息
          this.crownCADPlugin.query.getFeaturesByNames(["sketch1"])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {
              if (res.code=== 200 && res.data.features.length) {
                //获取草图sketch1中元素的信息
                this.crownCADPlugin.query.getElementsByFeatureId(res.data.features[0]?.id)
                // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETElEMENTSBYFEATURE, {featureId: res.features[0]?.id})
                  .then(result => {
                    result.data.elements.forEach(element => {
                      //获取渐开线信息，20类型为渐开线
                      if(element.type===8 && element.params.type=== 20) {
                        this.EquationId= element.id
                        this.EquationP= element.params.endPoint
                        this.EquationS= element.params.startPoint
                      }
                      //获取渐开线信息，20类型为渐开线
                      if(element.type===8 && element.params.type=== 11 && element.params.startPoint['0']=== 0.5* this.die) {
                        this.dieid.push(element.id)
                      }
                      //获取平齿根小径Die的id
                      if(element.type===8 && element.params.type=== 11 && element.params.startPoint['0']=== 0.5* this.dei) {
                        this.deiid.push(element.id)
                      }
                      //获取平齿根小径Die的id
                      if(element.type===8 && element.params.type=== 11 && element.params.startPoint['0']=== 0.5* this.deii) {
                        this.deiiid.push(element.id)
                      }
                      //获取分度圆的id
                      if(element.type===8 && element.params.type=== 11 && element.params.startPoint['0']=== 0.5* this.d) {
                        this.id1.push(element.id)

                      }
                      //获取平齿根大径Dee的id
                      if(element.type===8 && element.params.type=== 11 && element.params.startPoint['0']=== 0.5* this.dee) {
                        this.deeid.push(element.id)

                      }
                      //获取圆齿根小径Diee的id
                      if(element.type===8 && element.params.type=== 11 && element.params.startPoint['0']=== 0.5* this.diee) {
                        this.dieeid.push(element.id)

                      }
                      //获取平齿根内花键小径Dii的id
                      if(element.type===8 && element.params.type=== 11 && element.params.startPoint['0']=== 0.5* this.dii) {
                        this.diiid.push(element.id)

                      }


                    })
                    resolve();
                  })

              } else {
                reject()
              }
            })
        })

      },

      //剪切分度圆，因为要取分度圆和渐开线的交点，分度圆裁剪后会剩两者的交点
      cutPitchCircle() {
        this.crownCADPlugin.command.clearCommand();
        //进入编辑操作
        this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
        //裁剪分度圆
        this.crownCADPlugin.command.sketch.createAutoTrimCurve({
          trimCurveId: this.id1,
          trimCurvePoint: new Point( 0.5*this.d,0,0),
        });
        //退出编辑
        this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
        //执行编辑
        this.crownCADPlugin.command.execute().then(() => {
          //获取分度圆和渐开线交点
          this.getPolygonPoint().then(()=> {
            //获取创建内多边形
            this.createPolygon();
          });
        });
      },

      //获取分度圆和渐开线交点
      getPolygonPoint() {
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(["sketch1"])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {
              if (res.code=== 200 && res.data.features.length) {
                this.crownCADPlugin.query.getElementsByFeatureId(res.data.features[0]?.id)
                // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETElEMENTSBYFEATURE, {featureId: res.features[0]?.id})
                  .then(result => {
                    result.data.elements.forEach(element => {
                      //类型为12是曲线（裁剪分度圆得到的曲线），起点为渐开线和圆的交点
                      if (element.type === 8 && element.params.type ===12) {
                        this.q1.x= element.params.startPoint['0']
                        this.q1.y= element.params.startPoint['1']
                      }
                    })
                    resolve();
                  })
              }
            })
        })
      },

      //获取创建内多边形
      createPolygon() {
        this.crownCADPlugin.command.clearCommand();
        //进入编辑操作
        this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
        //创建内多边形
        const q1 = new Point(this.q1.x, this.q1.y);
        this.crownCADPlugin.command.sketch.createRegularPolygon({
          centerPoint: new Point(0,0,0),
          startPoint: q1,
          edgeNum: 2* this.z,
          mode: 0,
        })
        //退出编辑操作
        this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
        this.crownCADPlugin.command.execute().then(() => {
          this.crownCADPlugin.command.clearCommand();
          //创建渐开线镜像，需要先创建镜像线，镜像线为内多边形一边的中点该方法获取中点
          this.getAxisPoint().then(()=> {
            //创建镜像线
            this.createLine()
          })
        })
      },

      //创建渐开线镜像，需要先创建镜像线，镜像线为内多边形一边的中点该方法获取中点
      getAxisPoint() {
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(["sketch1"])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {
              if (res.code=== 200 && res.data.features.length) {
                this.crownCADPlugin.query.getElementsByFeatureId(res.data.features[0]?.id)
                // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETElEMENTSBYFEATURE, {featureId: res.features[0]?.id})
                  .then(result => {
                    result.data.elements.forEach(element => {
                      //内外花键获取点的方法
                      if(this.type=== 'out') {
                        if(element.type===8 && element.params.endPoint !== null) {
                          if(this.q1.x.toFixed(5)=== element.params.endPoint['0'].toFixed(5)) {
                            //mP存储内接多边形一边的中点为镜像线
                            this.mP=  element.params.middlePoint
                            this.q1.x= element.params.endPoint['0']
                            this.q1.y= element.params.endPoint['1']
                          }
                        }
                      } else {
                        if(element.type===8 && element.params.endPoint !== null) {
                          if(this.q1.x.toFixed(5)=== element.params.startPoint['0'].toFixed(5)) {
                            this.mP=  element.params.middlePoint
                            this.q1.x= element.params.startPoint['0']
                            this.q1.y= element.params.startPoint['1']
                          }
                        }
                      }
                      //类型10为直线，id2存储所有内接多边形的id，后期会删除这些线
                      if(element.type===8 && element.params.type=== 10 ) {
                        this.id2.push(element.id)
                      }
                    })
                    resolve();
                  })
              }
            })
        })
      },

      //创建镜像线
      createLine() {
        this.crownCADPlugin.command.clearCommand();
        this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
        // 创建镜像线
        this.crownCADPlugin.command.sketch.createSketchLine({
          startPoint: new Point(0,0,0),
          endPoint:new Point( this.mP['0'],this.mP['1'],0)
        })
        this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
        this.crownCADPlugin.command.execute().then(() => {
          this.crownCADPlugin.command.clearCommand();
          //获取镜像线id
          this.getLine().then(()=>{
            //创建渐开线镜像
            this.createEquationCurveMirror()
          })
        })

      },
      //获取镜像线id
      getLine() {
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(["sketch1"])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {
              if (res.code=== 200 && res.data.features.length) {
                this.crownCADPlugin.query.getElementsByFeatureId(res.data.features[0]?.id)
                // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETElEMENTSBYFEATURE, {featureId: res.features[0]?.id})
                  .then(result => {
                    result.data.elements.forEach(element => {
                      if (element.type === 8 && element.params.endPoint !== null) {
                        if (element.params.endPoint['0'] === this.mP['0']) {
                          this.axisId = element.id; //镜像线id
                          this.axisLen= element.params.length; //镜像线长度，后面计算圆齿根3点圆中间点使用
                        }
                      }
                    })
                    resolve();
                  })
              }
            })
        })
      },

      //创建渐开线镜像
      createEquationCurveMirror() {
        this.crownCADPlugin.command.clearCommand();
        this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
        //创建渐开线镜像
        this.crownCADPlugin.command.sketch.createMirror({
          elementIds: [this.EquationId],
          axisId: this.axisId,
        })
        this.crownCADPlugin.command.sketch.setReference({curveIds: [this.axisId], reference: 1})
        this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
        this.crownCADPlugin.command.execute().then(() => {
          this.crownCADPlugin.command.clearCommand();
          //获取镜像线信息
          this.getMirroLine().then(()=>{
            //裁剪各种线
            this.cutLine();
          })
        })
      },

      //获取镜像线信息
      getMirroLine() {
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(["sketch1"])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {
              if (res.code=== 200 && res.data.features.length) {
                this.crownCADPlugin.query.getElementsByFeatureId(res.data.features[0]?.id)
                // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETElEMENTSBYFEATURE, {featureId: res.features[0]?.id})
                  .then(result => {
                    result.data.elements.forEach(element => {
                      //类型20为镜像线，循环找到镜像的渐开线的id和起始点
                      if(element.type===8 && element.params.type=== 20) {
                        if(this.EquationId!== element.id) {
                          this.EquationId1= element.id;
                          this.EquationP1= element.params.endPoint;
                          this.EquationS1= element.params.startPoint;
                        }
                      }
                    })
                    resolve();
                  })
              }
            })
        })
      },

      //裁剪各种线
      cutLine() {
        this.crownCADPlugin.command.clearCommand();
        this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
        //裁剪已画的圆，裁剪至和渐开线、内多边形的交点
        if(this.type=== 'out') {
          if (this.rootType === '平齿根') {
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.dieid,
              trimCurvePoint: new Point( -0.5*this.die,0,0),
            })
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.deeid[0],
              trimCurvePoint: new Point( 0,0.5* this.dee,0),
            })
          } else {
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.dieid,
              trimCurvePoint: new Point( -0.5*this.die,0,0),
            })
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.dieeid[0],
              trimCurvePoint: new Point( 0,0.5* this.diee,0),
            })
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.deeid[0],
              trimCurvePoint: new Point( 0,0.5* this.dee,0),
            })
          }
        } else {
          if (this.rootType === '平齿根') {
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.deiid,
              trimCurvePoint: new Point( -0.5*this.dei,0,0),
            })
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.diiid[0],
              trimCurvePoint: new Point( -0.5* this.dii,0,0),
            })
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.deiid,
              trimCurvePoint: new Point( 0.5*this.dei,0,0),
            })
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.diiid[0],
              trimCurvePoint: new Point( 0.5* this.dii,0,0),
            })
          } else {
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.deiid,
              trimCurvePoint: new Point( -0.5*this.dei,0,0),
            })
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.deiiid,
              trimCurvePoint: new Point( -0.5*this.deii,0,0),
            })
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.diiid[0],
              trimCurvePoint: new Point( -0.5* this.dii,0,0),
            })
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.deiid,
              trimCurvePoint: new Point( 0.5*this.dei,0,0),
            })
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.deiiid,
              trimCurvePoint: new Point( 0.5*this.deii,0,0),
            })
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.diiid[0],
              trimCurvePoint: new Point( 0.5* this.dii,0,0),
            })

          }
        }


        this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
        this.crownCADPlugin.command.execute().then(() => {
          this.crownCADPlugin.command.clearCommand();
          this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
          //删除分度圆
          this.crownCADPlugin.command.sketch.deleteElements({
            elementIds: this.id1,
          })
          //删除内接多边形
          this.crownCADPlugin.command.sketch.deleteElements({
            elementIds: this.id2,
          })
          this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
          this.crownCADPlugin.command.execute().then(() => {
            this.crownCADPlugin.command.clearCommand();
            //再次获取镜像线信息，更新镜像线新的起始点，方便裁剪
            this.getMirroLine().then(()=>{
              this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
              //裁剪第一条渐近线到终点
              this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                trimCurveId: this.EquationId,
                trimCurvePoint: new Point( this.EquationP['0'],this.EquationP['1'],0),
              })
              //裁剪第二条渐近线到终点
              this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                trimCurveId: this.EquationId1,
                trimCurvePoint: new Point(this.EquationP1['0'],this.EquationP1['1'],0),
              })
              this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
              this.crownCADPlugin.command.execute().then(() => {
                //获取剩余线的信息
                this.getLeaveLine().then(()=> {
                  this.crownCADPlugin.command.clearCommand();
                  this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
                  if(this.type=== 'out') {
                    if (this.rootType === '平齿根') {
                      //裁剪第一条渐近线到起点
                      this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                        trimCurveId: this.EquationId,
                        trimCurvePoint: new Point(this.EquationS['0'],this.EquationS['1'],0),
                      })
                      //裁剪第二条渐近线到起点
                      this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                        trimCurveId: this.EquationId1,
                        trimCurvePoint: new Point( this.EquationS1['0'],this.EquationS1['1'],0),
                      })
                    } else {
                      //裁剪第一条渐近线到起点
                      this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                        trimCurveId: this.EquationId,
                        trimCurvePoint: new Point(this.EquationS['0'],this.EquationS['1'],0),
                      })
                      //裁剪第二条渐近线到起点
                      this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                        trimCurveId: this.EquationId1,
                        trimCurvePoint: new Point( this.EquationS1['0'],this.EquationS1['1'],0),
                      })

                      //相似三角形方法计算镜像线与diee交点
                      let x = this.mP['0']* 0.5 * this.diee/this.axisLen;
                      let y = this.mP['1']* 0.5 * this.diee/this.axisLen;

                      let poel1={};
                      let poel2={};
                      //找到平齿根小径曲线
                      this.poel.forEach(e => {
                        if(e.params.startPoint['0']> x && e.params.startPoint['0']< this.mP['0']) {
                          poel2= e;
                        }
                      })
                      //找到圆齿根小径曲线
                      this.poel.forEach(e => {
                        if(e.params.startPoint['0']< poel2.params.startPoint['0']) {
                          poel1= e;
                        }
                      })



                      //画三点圆
                      this.crownCADPlugin.command.sketch.createArcByThreePoint({
                        firstPoint: new Point(poel2.params.startPoint['0'],poel2.params.startPoint['1'],0),
                        secondPoint: new Point(poel2.params.endPoint['0'],poel2.params.endPoint['1'],0),
                        thirdPoint: new Point(x,y,0),
                      })
                      //删除多余的线
                      this.crownCADPlugin.command.sketch.deleteElements({
                        elementIds: [poel1.id, poel2.id],
                      })
                      //裁剪渐开线剩余部分
                      this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                        trimCurveId: this.EquationId,
                        trimCurvePoint: new Point(poel1.params.endPoint['0'],poel1.params.endPoint['1'],0),
                      })
                      this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                        trimCurveId: this.EquationId1,
                        trimCurvePoint: new Point(poel1.params.startPoint['0'],poel1.params.startPoint['1'],0),
                      })
                    }
                  } else {

                    //裁剪第一条渐近线到起点
                    this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                      trimCurveId: this.EquationId,
                      trimCurvePoint: new Point(this.EquationS['0'],this.EquationS['1'],0),
                    })
                    //裁剪第二条渐近线到起点
                    this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                      trimCurveId: this.EquationId1,
                      trimCurvePoint: new Point( this.EquationS1['0'],this.EquationS1['1'],0),
                    })
                    //裁剪渐开线剩余部分
                    this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                      trimCurveId: this.EquationId,
                      trimCurvePoint: new Point(this.q2.x,this.q2.y,0),
                    })
                    this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                      trimCurveId: this.EquationId1,
                      trimCurvePoint: new Point( this.q2.x,this.q2.y,0),
                    })
                    if (this.rootType === '圆齿根') {

                      //相似三角形方法计算镜像线与deii交点
                      let x1 = this.mP['0']* 0.5 * this.deii/this.axisLen;
                      let y1 = this.mP['1']* 0.5 * this.deii/this.axisLen;

                      let poel1={};
                      let poel2={};

                      //找到内花键平齿根大径曲线
                      this.poel.forEach(e => {
                        if(e.params.startPoint['0']< x1 && e.params.startPoint['0']> this.mP['0']) {
                          poel2= e;
                        }
                      })
                      //找到内花键圆齿根大径曲线
                      this.poel.forEach(e => {
                        if(e.params.startPoint['0']> poel2.params.startPoint['0']) {
                          poel1= e;
                        }
                      })



                      //画三点圆
                      this.crownCADPlugin.command.sketch.createArcByThreePoint({
                        firstPoint: new Point(poel2.params.startPoint['0'],poel2.params.startPoint['1'],0),
                        secondPoint: new Point(poel2.params.endPoint['0'],poel2.params.endPoint['1'],0),
                        thirdPoint: new Point(x1,y1,0),
                      })
                      //删除多余的线
                      this.crownCADPlugin.command.sketch.deleteElements({
                        elementIds: [poel1.id, poel2.id],
                      })
                      //裁剪渐开线剩余部分
                      this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                        trimCurveId: this.EquationId,
                        trimCurvePoint: new Point(poel1.params.startPoint['0'],poel1.params.startPoint['1'],0),
                      })
                      this.crownCADPlugin.command.sketch.createAutoTrimCurve({
                        trimCurveId: this.EquationId1,
                        trimCurvePoint: new Point(poel1.params.endPoint['0'],poel1.params.endPoint['1'],0),
                      })
                    }
                  }

                  this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
                  this.crownCADPlugin.command.execute().then(() => {
                    //创建花键轴和花键孔
                    this.createSplineShaftAndHole()
                  })

                })
              })
            })
          })
        })
      },

      //获取剩余线的信息
      getLeaveLine() {
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(["sketch1"])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {

                if (res.code=== 200 && res.data.features.length) {
                  this.crownCADPlugin.query.getElementsByFeatureId(res.data.features[0]?.id)
                // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETElEMENTSBYFEATURE, {featureId: res.features[0]?.id})
                  .then(result => {
                    //找到所有裁剪圆剩余的曲线
                    result.data.elements.forEach(element => {
                      if(element.type===8 && element.params.type=== 12) {
                        this.poel.push(element);
                      }
                      //找到内花键两条渐开线的交点
                      if(element.type===8 && element.params.type=== 20) {
                        this.q2.x= element.params.endPoint['0']
                        this.q2.y= element.params.endPoint['1']
                      }
                    })
                    resolve();
                  })
              }
            })
        })
      },

      //创建花键轴和花键孔
      createSplineShaftAndHole() {
        this.crownCADPlugin.command.clearCommand();
        //创建sketch2草图
        this.crownCADPlugin.command.sketch.createSketch({sketchName: 'sketch2', datumId: 8});
        if(this.type=== 'out') {
          //以Dee画外花键的轴
          this.crownCADPlugin.command.sketch.createCircle({
            centerPoint: new Point(0,0,0),
            circlePoint: new Point(0,0.5* this.dee,0),
          })
        } else {
          //以原点为圆心，以2*Dii和Dii为直径画两个圆，
          this.crownCADPlugin.command.sketch.createCircle({
            centerPoint: new Point(0,0,0),
            circlePoint: new Point(0,0.5* this.dii,0),
          })
          this.crownCADPlugin.command.sketch.createCircle({
            centerPoint: new Point(0,0,0),
            circlePoint: new Point(0,this.dii,0),
          })
        }
        this.crownCADPlugin.command.sketch.exitSketch();
        this.crownCADPlugin.command.execute().then(() => {
          //拉伸创建花键轴或者花键孔
          this.createExtrude().then(() => {
            //创建拉伸切除花键部分
            this.createExtrudeCut().then(()=>{
              this.createCircularPattern().then(()=>{
                this.loading = false;
              })
            })
          })
        })
      },

      //拉伸创建花键轴或者花键孔
      createExtrude() {
        this.crownCADPlugin.command.clearCommand();
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(["sketch2"])
            .then(res => {
            if (res.code=== 200 && res.data.features.length) {
              this.crownCADPlugin.query.getEntitiesByFeatureIds([res.data.features[0]?.id])
                .then(res => {
                if (res?.data.entities) {
                  this.crownCADPlugin.command.solid.extrude({
                    sketch: res.data.entities[0]?.id,
                    height1: this.alength,
                  }, "extrude");
                  this.crownCADPlugin.command.execute().then(() => {
                    return resolve();
                  });
                } else {
                  return reject();
                }
              });
            } else {
              return reject();
            }
          });
        });
      },

      //创建拉伸切除花键部分
      createExtrudeCut() {
        this.crownCADPlugin.command.clearCommand();
        return new Promise((resolve, reject) => {
          //获取草图sketch1中获得的齿形
          this.crownCADPlugin.query.getFeaturesByNames(["sketch1"])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {
              if (res.code=== 200 && res.data.features.length) {
                this.crownCADPlugin.query.getEntitiesByFeatureIds([res.data.features[0]?.id])
              // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETENTITIESBYFEATUREIDS, {featureIds: [res.features[0]?.id]})
                .then(res => {
                  if (res?.data.entities) {
                  //获取拉伸的特征
                  this.crownCADPlugin.query.getFeaturesByNames(["extrude"])
                  // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["extrude"]})
                    .then(extrudeRes => {
                    if (extrudeRes?.data.features) {
                      this.crownCADPlugin.query.getEntitiesByFeatureIds([extrudeRes.data.features[0]?.id])
                      // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETENTITIESBYFEATUREIDS, {featureIds: [extrudeRes.features[0]?.id]})
                        .then(ferRes => {
                        if (ferRes?.data.entities) {
                          //拉伸切除齿形
                          this.crownCADPlugin.command.solid.extrudeCut({
                            sketch: res.data.entities[0]?.id,
                            height1: this.alength,
                            cutSolids: [ferRes.data.entities[0]?.id]
                          }, "extrudeCut");
                          this.crownCADPlugin.command.execute().then(() => {
                            return resolve();
                          });
                        } else {
                          return reject();
                        }
                      });
                    } else {
                      return reject();
                    }
                  });
                } else {
                  return reject();
                }
              });
            } else {
              return reject();
            }
          });
        });
      },

      //花键阵列齿形
      createCircularPattern() {
        this.crownCADPlugin.command.clearCommand();
        //创建阵列中心轴
        let basePnt = new Point(0, 0, 0);
        let endPnt = new Point(0, 0, 10);
        const axis = new Axis();
        axis.setPoint(basePnt, endPnt);
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(["extrudeCut"])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["extrudeCut"]})
            .then(res => {
            if (res?.data.features) {
              //阵列创建阵列中心轴
              this.crownCADPlugin.command.solid.circularPattern({
                baseType: 1,
                features: [res.data.features[0]?.id],
                axis: axis,
                angle1: 360,
                instanceNum1: this.z,
              }, "");
              this.crownCADPlugin.command.execute().then(() => {
                return resolve();
              });
            } else {
              return reject();
            }
          });
        });
      }
    }
  }
</script>

<style scoped>
  >>> .el-form-item {
    margin-bottom: 0!important;
  }
  >>> .el-form-item__label {
    margin-bottom: 0!important;
  }
</style>
